/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */

/**
 * 获取完整的文件URL
 * @param filePath 服务器返回的文件路径
 * @returns 完整的文件URL
 */
export function getFullFileUrl(filePath: string): string {
  if (!filePath) {
    return ''
  }
  
  // 如果已经是完整URL，直接返回
  if (filePath.startsWith('http://') || filePath.startsWith('https://')) {
    return filePath
  }
  
  const env = import.meta.env
  const baseURL = env.VITE_OPEN_PROXY === 'true' 
    ? env.VITE_PROXY_PREFIX 
    : env.VITE_APP_API_BASEURL
  
  // 确保baseURL以斜杠结尾，filePath不以斜杠开头
  const normalizedBaseURL = baseURL.endsWith('/') ? baseURL.slice(0, -1) : baseURL
  const normalizedFilePath = filePath.startsWith('/') ? filePath : `/${filePath}`
  
  return normalizedBaseURL + normalizedFilePath
}

/**
 * 创建音频播放器元素
 * @param audioUrl 音频文件URL
 * @returns HTMLAudioElement
 */
export function createAudioPlayer(audioUrl: string): HTMLAudioElement {
  const audio = new Audio(audioUrl)
  audio.controls = true
  audio.preload = 'metadata'
  return audio
}

/**
 * 播放音频文件
 * @param audioUrl 音频文件URL
 */
export function playAudio(audioUrl: string): void {
  const audio = createAudioPlayer(audioUrl)
  audio.play().catch(error => {
    console.error('音频播放失败:', error)
  })
} 